// 云函数入口文件
const cloud = require('wx-server-sdk')
const htmlDocx = require('html-docx-js')
const fs = require('fs')
const path = require('path')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
})

// 云函数入口函数
exports.main = async (event, context) => {
  try {
    const { html, fileName = 'exported-document.docx' } = event
    
    // 1. 将HTML转换为DOCX
    const docx = htmlDocx.asBlob(html)
    
    // 2. 上传到云存储
    const fileStream = Buffer.from(docx)
    const uploadResult = await cloud.uploadFile({
      cloudPath: `temp_docx/${Date.now()}_${fileName}`,
      fileContent: fileStream
    })
    
    return {
      success: true,
      fileID: uploadResult.fileID
    }
  } catch (error) {
    console.error('云函数执行失败:', error)
    return {
      success: false,
      error: error.message
    }
  }
}